package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.constant.CiccTableNameEnum;
import com.chinairi.mall.controller.viewobject.ApproveDbIdVO;
import com.chinairi.mall.controller.viewobject.FourNewGoodsExVO;
import com.chinairi.mall.controller.viewobject.FourNewGoodsVO;
import com.chinairi.mall.model.CiccRecord;
import com.chinairi.mall.model.FourNewGoods;
import com.chinairi.mall.service.IFourNewGoodsService;
import com.chinairi.mall.service.IGoodsInfoService;
import com.chinairi.mall.util.CiccRecordUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 四新商品表 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-13
 */
@RestController
@RequestMapping("/four-new-goods")
public class FourNewGoodsController extends BaseController {

    @Autowired
    private IFourNewGoodsService iFourNewGoodsService;

    @Autowired
    private IGoodsInfoService iGoodsInfoService;


    /**
     * 获取四新列表
     *
     * @param request
     * @throws IOException
     */
    @GetMapping("/getList")
    public void getList(HttpServletRequest request) throws IOException {
        //获取实物店铺
        String csDbId = iGoodsInfoService.getCsDbid("0", request);
        FourNewGoodsVO fourNewGoodsVO = new FourNewGoodsVO();
        List<FourNewGoods> fourNewGoodsList = iFourNewGoodsService.getByCsDbId(fourNewGoodsVO, csDbId);
        List<FourNewGoodsVO> fourNewGoodsVOList = new ArrayList<>();
        BeanUtils.copyProperties(fourNewGoodsList, fourNewGoodsVOList);
        FourNewGoodsExVO fourNewGoodsExVO = new FourNewGoodsExVO();
        fourNewGoodsExVO.setCsDbid(csDbId);
        fourNewGoodsExVO.setFourNewGoodsList(fourNewGoodsList);
        ResponseUtil.showMessage("success", "获取四新商品成功", fourNewGoodsExVO);
    }

    /**
     * 新增四新商品
     *
     * @param fourNewGoodsVO
     * @throws IOException
     */
    @PostMapping("/add")
    public void add(@RequestBody FourNewGoodsVO fourNewGoodsVO) throws IOException {
        iFourNewGoodsService.add(fourNewGoodsVO);
        ResponseUtil.showMessage("success", "新增成功", "");
    }

    /**
     * 更新四新商品
     *
     * @param fourNewGoodsVO
     * @throws IOException
     */
    @PutMapping("/update")
    public void update(@RequestBody FourNewGoodsVO fourNewGoodsVO) throws IOException {
        iFourNewGoodsService.updateGoods(fourNewGoodsVO);
        ResponseUtil.showMessage("success", "更新成功", "");    
    }

    /**
     * 根据id查询四新商品所有信息
     * 包含特征量和图片
     *
     * @param fngDbid
     * @throws IOException
     */
    @GetMapping("/getById")
    public void getById(String fngDbid) throws IOException {
        FourNewGoods fourNewGoods = iFourNewGoodsService.getById(fngDbid);
        if (fourNewGoods !=null){
            CiccRecord ciccRecord = CiccRecordUtil.getCiccRecord(CiccTableNameEnum.GOODS_STATUS.getCodeValue(),String.valueOf(fourNewGoods.getApproveStatus()));
            fourNewGoods.setFngTypeLabel(CiccRecordUtil.getCiccRecord(CiccTableNameEnum.PRODUCT_TYPE.getCodeValue(),String.valueOf(fourNewGoods.getFngType())).getLabel());
            fourNewGoods.setApproveStatusLabel(ciccRecord.getLabel());
        }
        FourNewGoodsVO fourNewGoodsVO = iFourNewGoodsService.getFeatureAndPic(fngDbid);
        BeanUtils.copyProperties(fourNewGoods, fourNewGoodsVO);
        ResponseUtil.showMessage("success", "获取商品详细信息成功", fourNewGoodsVO);
    }

    /**
     * 删除某个商品
     */
    @DeleteMapping("/delete")
    public void delete(String fngDbid) throws IOException {
        iFourNewGoodsService.removeById(fngDbid);
        ResponseUtil.showMessage("success", "删除完成", "");
    }

    /**
     * 批量提交实物商品审核
     *
     * @param approveDbIdVoList
     * @throws IOException
     */
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody List<ApproveDbIdVO> approveDbIdVoList) throws IOException {
        iFourNewGoodsService.updateStatus(approveDbIdVoList);
        ResponseUtil.showMessage("success", "添加审核成功", null);
    }

    /**
     * 获取店铺四新商品
     *
     * @param fourNewGoods
     * @throws IOException
     */
    @GetMapping("/getListByCsDbId")
    public void getListByCsDbId(FourNewGoods fourNewGoods) throws IOException {
        List<FourNewGoods> fourNewGoodsList = iFourNewGoodsService.getListByCsDbId(fourNewGoods);
        ResponseUtil.showMessage("success", "获取四新商品成功", fourNewGoodsList);
    }
}
